

import { My } from '../my/my-model.js';
import { Agent } from '../agent/agent-model.js';
var my = new My();
var agent = new Agent();
var sliderWidth = 60;// 需要设置slider的宽度，用于计算中间位置
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loadingHidden: false,
    //推荐图标
    Recommend: "../../imgs/my/Recommend.png",
    yigou: "../../imgs/my/yigou.png",
    tabs: ["提现记录", "预期收入",],
    tabsDtail: ["已推荐", "已购车"],
    activeIndex: 0,
    activeIndex_s: 0,
    sliderOffset: 0,
    sliderLeft: 0,
    isScrollY: true,
    //推荐人data
    recommendData: '',
    //已购人
    toCraData: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;

    that._loadData();
  },
  _loadData: function () {
    var that = this;
    //获取用户信息
    my.getUserInfo((data) => {

      that.setData({
        userInfo: data,
        loadingHidden: true
      });

    })
    //页面加载获取二维码
    agent.selQrcodeMode((data) => {
      // console.log(data)
      that.setData({
        qrData: data
      })
    })
    //获取设备宽高
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          sliderLeft: (res.windowWidth / that.data.tabs.length - sliderWidth) / 2,
          sliderOffset: res.windowWidth / that.data.tabs.length * that.data.activeIndex,
          sliderLeftDtail: (res.windowWidth / that.data.tabsDtail.length - sliderWidth) / 2,
          sliderOffsetDtail: res.windowWidth / that.data.tabsDtail.length * that.data.activeIndex_s,
        });
      }
    });
    //页面加载查询推荐人信息
    agent.selRecommend((data) => {
      console.log(data);
      if (data.length == 0) {
        this.data.recommendData = 0;
      }
      else {
        this.data.recommendData = data;
        for (var i in data) {

          data[i]['create_time'] = agent.formatDateTime(data[i]['create_time'])
          switch (data[i]['state']) {
            case 0:
              data[i]['state'] = '--'
              break;
            case 1:
              data[i]['state'] = '等待预约'
              break;
            case 2:
              data[i]['state'] = '已签约，等待提车'
              break;
            case 4:
              data[i]['state'] = '推荐失败'
              break;
            default:
              data[i]['state'] = '未知状态'
              data[i]['notes'] = '已被他人推荐'
              break;
          } 
        }
      }
      this.setData({
        recommendData: this.data.recommendData,
        recommendNumber: data.length
      })
    })
    //查询已购车信息 where条件3
    agent.selToCraData((data) => {
      if (data.length == 0) {
        this.data.toCraData = 0
      } else {
        this.data.toCraData = data;
        for (var i in data) {
          data[i]['create_time'] = agent.formatDateTime(data[i]['create_time'])
          data[i]['state'] = '已提车'

        }
      }
      this.setData({
        toCraData: this.data.toCraData,
        toCraDataNumber: data.length

      })

    })


  },
  tabClick: function (e) {
    this.setData({
      sliderOffset: e.currentTarget.offsetLeft,
      activeIndex: e.currentTarget.id
    });
  },
  tabClickDtail: function (e) {
    this.setData({
      sliderOffsetDtail: e.currentTarget.offsetLeft,
      activeIndex_s: e.currentTarget.id
    });
  },
  //提现更多详情
  moneyMore: function () {
    this.setData({
      dalogShow: "zan-popup--show"
    })
  },
  //点击遮罩消失
  dialog_hide: function () {
    this.setData({
      dalogShow: ''

    })
  },
  //获取二维码
  setQrcode: function (e) {

    agent.setQrCodeModel((data) => {

      console.log(data);
      this.setData({
        qrData: data
      })
    })

  },
  //分享二维码
  previewImage: function (e) {
    wx.previewImage({
      urls: [e.currentTarget.dataset.url]
    })
  },
  //跳转到提现页面
  withdrawals: function (e) {
    wx.navigateTo({
      url: '../withdrawals/withdrawals'
    })
  },

  //已购、推荐弹出层
  recommend_detail: function (e) {
    this.setData({
      recommend_detail_dialog: 'zan-popup--show'
    })
  },

  dialog_hide_recommend: function () {
    this.setData({
      recommend_detail_dialog: ''

    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var page = getCurrentPages().pop();
    if (page == undefined || page == null) return;
    page._loadData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})